package game.module.hall.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.DaoCommonHandler;
import game.db.DataSourceManager;
import game.lan.bean.LoginSessionBean;
import game.lan.bean.OpenLoginSessionBean;
import game.module.hall.bean.SettingBean;
import game.module.user.bean.PlayerBean;

public class SettingDao {

	private static Logger logger = LoggerFactory.getLogger(SettingDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static SettingDao instance = new SettingDao();
	}

	public static SettingDao getInstance() {
		return SingletonHolder.instance;
	}

	private ResultSetHandler<SettingBean> aSettingHandler = new ResultSetHandler<SettingBean>() {
		@Override
		public SettingBean handle(ResultSet rs) throws SQLException {
			SettingBean settingBean = null;
			if (rs.next()) {
				settingBean = new SettingBean();
				settingBean.setId(rs.getInt("id"));
				settingBean.setPlayerId(rs.getInt("playerid"));
				settingBean.setVoice(rs.getBoolean("voice"));
				settingBean.setMsg(rs.getBoolean("msg"));
				settingBean.setClubInvite(rs.getBoolean("club_invite"));
			}
			return settingBean;
		}
	};

	private ResultSetHandler<List<SettingBean>> multiPlayerHandler = new ResultSetHandler<List<SettingBean>>() {
		@Override
		public List<SettingBean> handle(ResultSet rs) throws SQLException {
			List<SettingBean> userBeanList = new ArrayList<SettingBean>();
			while (rs.next()) {
				SettingBean settingBean = new SettingBean();
				settingBean.setId(rs.getInt("id"));
				settingBean.setPlayerId(rs.getInt("playerid"));
				settingBean.setVoice(rs.getBoolean("voice"));
				settingBean.setMsg(rs.getBoolean("msg"));
				settingBean.setClubInvite(rs.getBoolean("club_invite"));
				userBeanList.add(settingBean);
			}
			return userBeanList;
		}
	};

	public SettingBean getSettingById(int playerId) {
		SettingBean ret = null;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from hallsetting where playerid = ?", aSettingHandler, playerId);
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

	public void updateSetting(SettingBean settingBean) {
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			runner.update("update hallsetting set voice=?,msg=?,club_invite=? where id = ?", settingBean.isVoice(),
					settingBean.isMsg(), settingBean.isClubInvite(), settingBean.getId());
		} catch (SQLException e) {
			logger.error("", e);
		}
	}

	public boolean addSetting(SettingBean settingBean) {
		boolean addRet = false;
		QueryRunner runner = new QueryRunner(dataSource);
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			int ret = runner.update(conn,
					"insert into hallsetting(playerid,voice,msg,club_invite) " + "values(?,?,?,?)",
					settingBean.getPlayerId(), settingBean.isVoice(), settingBean.isMsg(), settingBean.isClubInvite());
			if (ret > 0) {
				addRet = true;
			}
			if (addRet) {
				int theId = runner.query(conn, "SELECT LAST_INSERT_ID();", DaoCommonHandler.Integerhandler);
				settingBean.setId(theId);
			}
		} catch (SQLException e) {
			logger.error("", e);
		} finally {
			DbUtils.closeQuietly(conn);
		}
		return addRet;

	}

}
